// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
    env: 'cloud1-4gqogyksa87ef4e2'
});
const db = cloud.database();
const wqb_talk = db.collection('wqb_talk'); // 存放讨论的内容
const wqb_user = db.collection('wqb_user'); // 用户信息
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
    const { scene } = event;
    // 场景
    return new Promise(async (reslove, reject) => {
        // 发布一条讨论信息
        if (scene === 'SAVE_TALK') {
            const { uid, content, imgUrl } = event;
            // 优化，根据 uid，先获取用户信息
            const userInfo = await wqb_user.doc(uid).get();
            const { nickname, avatar } = userInfo.data;
            wqb_talk.add({
                data: {
                    uid,
                    nickname,
                    avatar,
                    content, // 发布的内容
                    imgUrl, // 发布的图片
                    commentArr: [], // 存放回复的数组
                    likeArr: [], // 存放点赞到的数组
                    creatDate: Date.now(),
                }
            })
                .then(res => {
                    reslove(res);
                });
        }

        //获取数据库里所有的信息
        if (scene === 'GET_ALL_TALK') {
            const { skipNum, limitNum } = event;
            const res = await wqb_talk
                .orderBy('creatDate', 'desc')
                .skip(skipNum)
                .limit(limitNum)
                .get()
            reslove(res);
        }

        // 获取一条讨论的详情信息
        if (scene === 'GET_ONE_TALK_DETAIL') {
            const { tid } = event;
            const res = await wqb_talk.doc(tid).get();
            reslove(res);
        }

        // 进行评论
        if (scene === 'ADD_ONE_COMMENT') {
            const { tid, commentVal, commentImgUrl, nickname, avatar, uid } = event;
            const res = await wqb_talk.doc(tid).update({
                data: {
                    commentArr: _.unshift([{
                        commentVal,
                        commentImgUrl,
                        nickname,
                        avatar,
                        uid,
                        creatDate: Date.now(),
                    }])
                }
            })
            reslove(res);
        }

        // 点赞接口
        if (scene === 'DEFINE_LIKE') {
            const { tid, uid } = event;
            const res1 = await wqb_talk.doc(tid).update({
                data: {
                    likeArr: _.push([uid]),
                }
            });
            const res2 = await wqb_user.doc(uid).update({
                data: {
                    likeArr: _.push([tid]),
                }
            })
            reslove({ update1: res1, update2: res2 });
        }

        // 取消点赞
        if (scene === 'CANCEL_LIKE') {
            const { tid, uid } = event;
            const res1 = await wqb_talk.doc(tid).update({
                data: {
                    likeArr: _.pullAll([uid]),
                }
            });
            const res2 = await wqb_user.doc(uid).update({
                data: {
                    likeArr: _.pullAll([tid]),
                }
            })
            reslove({ update1: res1, update2: res2 });
        }

        /* 场景为获取用户发布的讨论的数量 */
        if (scene === 'GET_TALK_COUNT') {
            const { uid } = event;
            const res = await wqb_talk.where({
                uid,
            }).count();
            reslove(res);
        }

        // 获取此用户，20条讨论动态
        if (scene === 'GET_APPOINT_USER_TALK') {
            const { skipNum, limitNum, uid } = event;
            const res = await wqb_talk.where({
                uid,
            })
                .orderBy('creatDate', 'desc')
                .skip(skipNum)
                .limit(limitNum)
                .get()
            reslove(res);
        }

        /* 场景为根据uid，此用户点过赞的talk内容 */
        if (scene === 'GET_TALK_LIKE_LIST') {
            const { uid } = event;
            const res = await wqb_talk.where({
                likeArr: _.elemMatch(_.eq(uid)),
            }).get();
            reslove(res);
        }


    });
}